//
//  MJLoginView.m
//  MJ
//
//  Created by qiaoBu on 2017/12/11.
//  Copyright © 2017年 lina. All rights reserved.
//

#import "MJLoginView.h"

@interface MJLoginView ()<UITextFieldDelegate,UIAlertViewDelegate>

@end

@implementation MJLoginView


- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        [self commonInit];
        
    }
    return self;
}
- (void)commonInit{
    
    UIView *topLine = [[UIView alloc] init];
    [self addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
    topLine.backgroundColor = RGBColor(229,229,229);
    
    _telephoneField = [[UITextField alloc] init];
    [self addSubview:_telephoneField];
    _telephoneField.delegate = self;
    [_telephoneField mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.right.mas_equalTo(self);
        make.top.mas_equalTo(topLine.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    _telephoneField.font = [UIFont systemFontOfSize:14];
    _telephoneField.placeholder = @"请输入手机号";
    _telephoneField.returnKeyType = UIReturnKeyDone;
    [_telephoneField setValue:RGBColor(217,217,217) forKeyPath:@"_placeholderLabel.textColor"];
    _telephoneField.delegate = self;
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 55)];
    leftView.backgroundColor = [UIColor whiteColor];
    _telephoneField.leftView = leftView;
    _telephoneField.leftViewMode = UITextFieldViewModeAlways;
    
    
    UIView *centerLine = [[UIView alloc] init];
    [self addSubview:centerLine];
    [centerLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.height.mas_equalTo(topLine);
        make.top.mas_equalTo(_telephoneField.mas_bottom);
    }];
    centerLine.backgroundColor = RGBColor(229,229,229);
    
    _verifyCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_verifyCodeBtn];
    [_verifyCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(centerLine.mas_bottom).offset(10);
        make.right.mas_equalTo(self.mas_right).offset(-10);
        make.height.mas_equalTo(34);
        make.width.mas_equalTo(130);
    }];
    [_verifyCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    _verifyCodeBtn.titleLabel.font = [UIFont systemFontOfSize:14.f];
    [_verifyCodeBtn setTitleColor:RGBColor(255,130,128) forState:UIControlStateNormal];
    _verifyCodeBtn.backgroundColor = [UIColor whiteColor];
    _verifyCodeBtn.layer.masksToBounds = YES;
    _verifyCodeBtn.layer.cornerRadius = 30/2.0f;
    _verifyCodeBtn.layer.borderWidth = 1.0f;
    _verifyCodeBtn.layer.borderColor =  RGBColor(255,130,128).CGColor;
     [_verifyCodeBtn addTarget:self action:@selector(requestVerificationCodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    _verifyCodeField = [[UITextField alloc] init];
    [self addSubview:_verifyCodeField];
    [_verifyCodeField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.height.mas_equalTo(_telephoneField);
        make.top.mas_equalTo(centerLine.mas_bottom);
        make.right.mas_equalTo(_verifyCodeBtn.mas_left);
    }];
    _verifyCodeField.font = [UIFont systemFontOfSize:14];
    _verifyCodeField.placeholder = @"请输入验证码";
    [_verifyCodeField setValue:RGBColor(217,217,217) forKeyPath:@"_placeholderLabel.textColor"];
     _verifyCodeField.delegate = self;
    _verifyCodeField.returnKeyType = UIReturnKeyDone;
    
    UIView *leftCodeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 55)];
    leftCodeView.backgroundColor = [UIColor whiteColor];
    _verifyCodeField.leftView = leftCodeView;
    _verifyCodeField.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *bottomLine = [[UIView alloc] init];
    [self addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(0.5);
        make.top.mas_equalTo(self.verifyCodeField.mas_bottom);
    }];
    bottomLine.backgroundColor = RGBColor(229,229,229);
    
    UIView *backGrayView = [[UIView alloc] init];
    [self addSubview:backGrayView];
    backGrayView.backgroundColor = RGBColor(242,242,242);
    [backGrayView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(bottomLine.mas_bottom);
        make.left.right.bottom.mas_equalTo(self);
    }];
    _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backGrayView addSubview:_loginBtn];
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mas_right).offset(-15);
        make.left.mas_equalTo(self.mas_left).offset(15);
        make.height.mas_equalTo(85/2.0);
        make.top.mas_equalTo(backGrayView.mas_top).offset(50);
    }];
    [_loginBtn addTarget:self action:@selector(loginBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _loginBtn.clipsToBounds = YES;
    _loginBtn.layer.cornerRadius = 85/4.0;
    _loginBtn.backgroundColor = RGBColor(255,130,128);
    _loginBtn.titleLabel.font = [UIFont systemFontOfSize:18.f];

    
    _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backGrayView addSubview:_registerBtn];
    [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mas_right).offset(-5);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(backGrayView.mas_top).offset(10);
    }];
    [_registerBtn addTarget:self action:@selector(registerBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_registerBtn setTitleColor:RGBColor(191,191,191) forState:UIControlStateNormal];
    [_registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    _registerBtn.titleLabel.font = [UIFont systemFontOfSize:12];
}



- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [_telephoneField resignFirstResponder];
    [_verifyCodeField resignFirstResponder];
    return YES;
    
}



- (void)requestVerificationCodeBtnClick:(UIButton *)button{
    
    [_telephoneField resignFirstResponder];
    [_verifyCodeField resignFirstResponder];
    
    //如果验证输入为空白,则提示:
     if (self.requestVerificationCodeBlcok){
            
            self.requestVerificationCodeBlcok([NSString stringWithFormat:@"%@",_telephoneField.text]);
        }
}
- (void)loginBtnClick:(UIButton *)button{
    
    [_telephoneField resignFirstResponder];
    [_verifyCodeField resignFirstResponder];
    
        if (self.verificationCodeLoginBlock){
            
            self.verificationCodeLoginBlock([NSString stringWithFormat:@"%@",_telephoneField.text],[NSString stringWithFormat:@"%@",_verifyCodeField.text]);
        }
}
- (void)registerBtnClick:(UIButton *)button{
    
    [_telephoneField resignFirstResponder];
    [_verifyCodeField resignFirstResponder];
    
    if (self.registerBlock)
    {
        self.registerBlock();
    }
}

@end
